package com.richest.api.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

import com.github.wxpay.sdk.WXPayConstants;

/**
 * 
 * @author zhaohongchao 2019年4月4日 上午10:30:11
 * @email 70375739@qq.com
 */
public class WxMD5Util {
	public String getSign(Map<String, String> data) throws Exception {
		WXConfigUtil config = new WXConfigUtil();
		Set<String> keySet = data.keySet();
		String[] keyArray = keySet.toArray(new String[keySet.size()]);
		Arrays.sort(keyArray);
		StringBuilder sb = new StringBuilder();
		for (String k : keyArray) {
			if (k.equals(WXPayConstants.FIELD_SIGN)) {
				continue;
			}
			if (data.get(k).trim().length() > 0) // 参数值为空，则不参与签名
				sb.append(k).append("=").append(data.get(k).trim()).append("&");
		}
		sb.append("key=").append(config.getKey());
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		byte[] array = new byte[0];
		try {
			array = md.digest(sb.toString().getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		StringBuilder sb2 = new StringBuilder();
		for (byte item : array) {
			sb2.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
		}
		return sb2.toString().toUpperCase();
	}
}